﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mrsoft.Common;
using Mrsoft.Model;

namespace Mrsoft.Core {
    class BaitanConfig {
        //用于记录信息
        private string user;

        public BaitanConfig() {
            this.PriceMap = new Dictionary<string, string>();
            this.Map = "";
            this.BaitanLocation = new XY();
        }

        public static BaitanConfig Create(string user) {
            user = string.Format("{0}/json/baitan", user);
            BaitanConfig v;
            if (!XmlSaver<BaitanConfig>.FromXml(user, out v)) {
                v = new BaitanConfig();
            }
            v.user = user;
            return v;
        }

        private void Save() {
            XmlSaver<BaitanConfig>.Save(this.user, this);
        }

        public void SetPrice(string name, string price) {
            if (PriceMap.ContainsKey(name)) {
                PriceMap[name] = price;
            }
            else {
                PriceMap.Add(name, price);
            }
            this.Save();
        }

        public string Map { set; get; }


        public string ShowMapAndLocation() {
            return string.Format("[{0}]({1},{2})", Map, BaitanLocation.X, BaitanLocation.Y);
        }

        public List<string> ShowPriceMap() {
            var list = new List<string>();
            foreach (var li in PriceMap) {
                list.Add(string.Format("{0}|{1}", li.Key, li.Value));
            }
            return list;
        }


        public Dictionary<string, string> PriceMap { get; private set; }

        public XY BaitanLocation {
            set;
            get;
        }

        public void SetLocation(string map, XY xy) {
            this.Map = map;
            this.BaitanLocation = xy;
            this.Save();
        }
    }
}
